<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="700px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="150px">
          <el-form-item label="用户" prop="yxUserId">
            <el-input
              v-if="false"
              v-model="form.yxUserId"
              style="width: 370px;"
            />
            <div v-if="form.yxUserId">
              {{ dialogTableDataYxUserId.showInputValue ? dialogTableDataYxUserId.showInputValue : form.username }}
            </div>
            <!-- 新增组件弹出式表单 -->
            <dialogTable
              :dialogTableData.sync="dialogTableDataYxUserId"
              @select-callback="dialogTableSelectedCallBackYxUserId"
            ></dialogTable>
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="学号">
            <el-input v-model="form.studentCode" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="学生证照片">
            <MaterialList
              v-model="imagesArr"
              style="width: 915px;"
              type="image"
              :num="1"
              :width="150"
              :height="150"
            />
          </el-form-item>
          <el-form-item label="电话" prop="studentPhone">
            <el-input v-model="form.studentPhone" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="性别" prop="studentSex">
            <el-select v-model="form.studentSex" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.SEX"
                :key="item.id"
                :label="item.label"
                :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="学校" prop="schoolName">
            <el-input v-model="form.schoolName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="校区" prop="campusId">
            <el-input
              v-if="false"
              v-model="form.campusId"
              style="width: 370px;"
            />
            <div v-if="form.campusId">
              {{ dialogTableDataCampusId.showInputValue ? dialogTableDataCampusId.showInputValue : form.campusName }}
            </div>
            <!-- 新增组件弹出式表单 -->
            <dialogTable
              :dialogTableData.sync="dialogTableDataCampusId"
              @select-callback="dialogTableSelectedCallBackCampusId"
            ></dialogTable>
          </el-form-item>
          <el-form-item label="年级">
            <el-select v-model="form.grade" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.BASE_TEAM_GRADE"
                :key="item.id"
                :label="item.label"
                :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="宿舍楼栋">
            <el-input v-model="form.dormBuilding" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="宿舍号">
            <el-input v-model="form.dormBuildingNum" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select v-model="form.status" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.BASE_TEAM_STATUS"
                :key="item.id"
                :label="item.label"
                :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="校园合伙人" prop="teamPartner">
            <el-input
              v-if="false"
              v-model="form.teamPartner"
              style="width: 370px;"
            />
            <div v-if="form.teamPartner">
              {{ dialogTableDataTeamPartner.showInputValue ? dialogTableDataTeamPartner.showInputValue : form.teamPartnerName }}
            </div>
            <!-- 新增组件弹出式表单 -->
            <dialogTable
              :dialogTableData.sync="dialogTableDataTeamPartner"
              @select-callback="dialogTableSelectedCallBackTeamPartner"
            ></dialogTable>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('yxUserId')" prop="yxUserId" label="用户微信ID"/>
        <el-table-column v-if="columns.visible('name')" prop="name" label="姓名" />
        <el-table-column v-if="columns.visible('studentCode')" prop="studentCode" label="学号" />
        <el-table-column v-if="columns.visible('studentPhone')" prop="studentPhone" label="电话" width="105" />
        <el-table-column v-if="columns.visible('studentSex')" prop="studentSex" label="性别" width="55" >
          <template slot-scope="scope">
            {{ dict.label.SEX[scope.row.studentSex] }}
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('grade')" prop="grade" label="年级" width="55">
          <template slot-scope="scope">
            {{ dict.label.BASE_TEAM_GRADE[scope.row.grade] }}
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('dormBuilding')" prop="dormBuilding" label="宿舍楼栋"  width="75"/>
        <el-table-column v-if="columns.visible('dormBuildingNum')" prop="dormBuildingNum" label="宿舍号"  width="65"/>
        <el-table-column v-if="columns.visible('status')" prop="status" label="状态" width="65">
          <template slot-scope="scope">
            {{ dict.label.BASE_TEAM_STATUS[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('teamPartnerName')" prop="teamPartnerName" label="校园合伙人" />
        <el-table-column v-if="columns.visible('createTime')" prop="createTime" label="加入时间" width="185">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('remark')" prop="remark" label="备注"  width="80px"/>
        <el-table-column v-permission="['admin','角色']" label="角色" width="80px" align="center">
          <template slot-scope="scope">
<!--              <dialogTable-->
<!--                      :dialogTableData.sync="dialogTableDataTeamRoleAllocation"-->
<!--                      @select-callback="dialogTableSelectedCallBackTeamRoleAllocation"-->
<!--              ></dialogTable>-->
            </template>
        </el-table-column>
        <el-table-column v-permission="['admin','tanzerBaseTeam:edit','tanzerBaseTeam:del']" label="操作" width="120px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudTanzerBaseTeam from '@/api/tanzer/tanzerBaseTeam'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import MaterialList from "@/components/material";
import dialogTable from "@/views/components/dialogTable";

// crud交由presenter持有
const defaultCrud = CRUD({ title: '团队信息表', url: 'api/tanzerBaseTeam', sort: 'id,desc', crudMethod: { ...crudTanzerBaseTeam }})
const defaultForm = { id: null, yxUserId: null, name: null, studentCode: null, studentCodePic: null, studentPhone: null, studentSex: null, school: null, grade: null, dormBuilding: null, dormBuildingNum: null, stauts: null, teamPartner: null, remark: null, isDel: null, createTime: null, createBy: null, updateBy: null, updateTime: null }
export default {
  name: 'TanzerBaseTeam',
  components: { dialogTable, pagination, crudOperation, rrOperation, udOperation ,MaterialList},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  dicts: ['SEX', 'BASE_TEAM_GRADE', 'BASE_TEAM_STATUS'],
  data() {
    return {
      imagesArr:[],
      permission: {
        add: ['admin', 'tanzerBaseTeam:add'],
        edit: ['admin', 'tanzerBaseTeam:edit'],
        del: ['admin', 'tanzerBaseTeam:del']
      },
      rules: {
        yxUserId: [
          { required: true, message: '微信id不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        studentPhone: [
          { required: true, message: '电话不能为空', trigger: 'blur' }
        ],
        studentSex: [
          { required: true, message: '性别不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态不能为空', trigger: 'blur' }
        ],
      },
      queryTypeOptions: [
        { key: 'yxUserId', display_name: '微信id' },
        { key: 'name', display_name: '姓名' },
        { key: 'studentPhone', display_name: '电话' },
        { key: 'status', display_name: '状态' },
        { key: 'teamPartner', display_name: '校园合伙人id（当前表id）' }
      ],

      //用户数据源
      dialogTableDataYxUserId: {
        title: "用户信息",
        dialogVisible: false,
        url: "api/yxUser",
        sort: "uid,desc",
        buttonContext: "选择用户",
        showInputValue: "",
        queryTypeOptions: [
          { key: 'username', display_name: '用户账户' },
          { key: 'realName', display_name: '用户姓名' },
        ],
        elTtableColumns: [
          {
            columnProp: "username",
            columnLabel: "用户账户"
          },
          {
            columnProp: "realName",
            columnLabel: "用户姓名"
          },
          {
            columnProp: "level",
            columnLabel: "等级"
          },
          {
            columnProp: "userType",
            columnLabel: "用户类型"
          }
        ]
      },

      //校区数据源
      dialogTableDataCampusId: {
        title: "校区信息",
        dialogVisible: false,
        url: "api/tanzerBaseCampus",
        sort: "id,desc",
        buttonContext: "选择校区",
        showInputValue: "",
        queryTypeOptions: [
          { key: 'schoolName', display_name: '学校名称' },
          { key: 'campusName', display_name: '校区名称' }
        ],
        elTtableColumns: [
          {
            columnProp: "schoolName",
            columnLabel: "学校名称"
          },
          {
            columnProp: "campusName",
            columnLabel: "校区名称"
          }
        ]
      },

      //团队成员数据源
      dialogTableDataTeamPartner: {
        title: "合伙人",
        dialogVisible: false,
        url: "/api/tanzerBaseTeam/selectTeamPartner",
        sort: "id,desc",
        buttonContext: "选择合伙人",
        showInputValue: "",
        queryTypeOptions: [
          { key: 'name', display_name: '姓名' },
        ],
        elTtableColumns: [
          {
            columnProp: "name",
            columnLabel: "姓名"
          },
          {
            columnProp: "schoolName",
            columnLabel: "学校"
          }
        ]
      },
    }
  },
  watch: {
    "imagesArr":function(val) {
      if (val) {
        this.form.studentCodePic = val.join(",");
      }
    },
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }else{
        delete this.crud.params.yxUserId
        delete this.crud.params.name
        delete this.crud.params.studentPhone
        delete this.crud.params.studentSex
        delete this.crud.params.school
        delete this.crud.params.dormBuilding
        delete this.crud.params.status
        delete this.crud.params.teamPartner
      }
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      if (form.images && form.id) {
        this.imagesArr = form.images.split(",");
      }else{
        this.imagesArr=[]
      }
    },

    //用户选择
    dialogTableSelectedCallBackYxUserId(row) {
      this.form.yxUserId = row.uid;
      if (this.form.yxUserId) {
        this.dialogTableDataYxUserId.showInputValue = row.realName;
      }
    },

    //校区选择
    dialogTableSelectedCallBackCampusId(row) {
      this.form.campusId = row.id;
      if (this.form.campusId) {
        this.form.schoolId = row.id;
        this.form.schoolName = row.schoolName;
        this.dialogTableDataCampusId.showInputValue = row.campusName;
      }
    },

    //合伙人选择
    dialogTableSelectedCallBackTeamPartner(row) {
      this.form.teamPartner = row.id;
      if (this.form.teamPartner) {
        this.dialogTableDataTeamPartner.showInputValue = row.name;
      }
    },
  }
}



</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
</style>
